<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">

                <el-form-item label="总数" prop="total">
                    <el-input v-model="formData.total" clearable placeholder="总数" />
                </el-form-item>
                <el-form-item label="归属商户" prop="merchant_id">
                    <el-select v-model="formData.merchant_id" value-key="" placeholder="" clearable filterable>
                        <el-option v-for="item in state.merchantList" :key="item.id" :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>

                </el-form-item>


                <el-form-item label="产品" prop="product_id">
                    <el-checkbox-group v-model="formData.product_id">
                        <el-checkbox v-for="item in state.items" :key="item.id" :label="item.id">
                            {{ item.name }}
                        </el-checkbox>
                    </el-checkbox-group>

                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="cardEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCardAdd, apiCardEdit, apiCardDetail } from '@/api/card'

import { apiProductTypeAll } from '@/api/product_type'
import { apiMerchantAll } from '@/api/merchant'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑自助卡' : '新增自助卡'
})

const state = reactive({
    items: [

    ],
    merchantList: []
})



// 表单数据
const formData = reactive({
    merchant_id: '',
    total: '',
    product_id: [],
})


// 表单验证
const formRules = reactive<any>({
    product_id: [{
        required: true,
        message: '请选择产品',
        trigger: ['blur']
    }],
    merchant_id: [{
        required: true,
        message: '请输入归属商户',
        trigger: ['blur']
    }],
    total: [{
        required: true,
        message: '请输入总数',
        trigger: ['blur']
    }]
})


// 获取详情





// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    await apiCardAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    console.log(33333333);

    popupRef.value?.open()


    apiProductTypeAll({}).then(res => {
        state.items = res
    })
    apiMerchantAll({}).then(res => {
        state.merchantList = res
    })
    formData.product_id = []
    formData.merchant_id = ''
    formData.total = ''
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
})
</script>
